#include "file.h"
#include "order/orderer.h"

#define UNSORTED_FILE_NAME "txt/unordered_numbers.txt"
  
void (*sort_functions[NUM_ORDER_FUNCTIONS]) (int* array, const int length) = 
  {bubble_sort, selection_sort, insertion_sort, shell_sort, quick_sort, heap_sort};
  
void get_option(unsigned int* opt) {
  printf("The options are:\n");
  printf("0 - Bubble Sort  |  1 - Selection Sort  |  2 - Insertion Sort\n");
  printf("3 - Shell Sort   |  4 - Quick Sort      |  5 - Heap Sort\n");
  printf("6 - Default File |  7 - Random File     |  9 - Quit\n");
  printf("Enter with the option to execute: ");
  scanf("%u", opt);
}
  
void execute_function(function func, char* algorithm) {
  
  int* array_numbers = NULL;
  unsigned int length = get_stream_from_file(UNSORTED_FILE_NAME, &array_numbers);
  printf("Printing array that has been ordered by %s algorithm ...\n", algorithm);
  sort_functions[func](array_numbers, length);
  print_array(array_numbers, length);
}
  
int main(int argc, char **argv) {
  
  unsigned int input_length = 0;
  unsigned int option;
  
  get_option(&option);
  
  while (option >= 0 && option < 9) {  
  
    switch (option) {
      
      case func_bubble_sort : {
	execute_function(func_bubble_sort, "BUBBLE SORT");
	break;
      }
      case func_selection_sort : {
	execute_function(func_selection_sort, "SELECTION SORT");
	break;
      }
      case func_insertion_sort : {
	execute_function(func_insertion_sort, "INSERTION SORT");
	break;
      }
      case func_shell_sort : {
	execute_function(func_shell_sort, "SHELL SORT");
	break;
      }
      case func_quick_sort : {
	execute_function(func_quick_sort, "QUICK SORT");
	break;
      }
      case func_heap_sort : {
	execute_function(func_heap_sort, "HEAP SORT");
	break;
      }
      case 6 : {
	load_default_file_values(UNSORTED_FILE_NAME);
	printf("Default file loaded.\n");
	break;
      }
      case 7 : {
	printf("Enter the size of array to order: ");
	scanf("%u", &input_length);
	load_random_file_values(UNSORTED_FILE_NAME, input_length);
	printf("Random file loaded.\n");
	break;
      }
      default : {
	break;
      }
    }
    
    get_option(&option);
  }
  
  exit(0);
}



